package com.hainatong.shiphelper.module.warehouse.viewmodel;


import com.hainatong.shiphelper.api.ApiResult;
import com.hainatong.shiphelper.api.ApiService;
import com.hainatong.shiphelper.api.VMObserver;
import com.hainatong.shiphelper.module.warehouse.entity.ImpaBean;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.base.LiveResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.disposables.Disposable;

public class RuKuLogViewModel extends BaseViewModel
{

    public RuKuLogViewModel(@NonNull Application application)
    {
        super(application);
    }


    private MutableLiveData<LiveResult<ImpaBean>> impaResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<ImpaBean>> getImpaResult()
    {
        return impaResult;
    }

    /**
     * 查 物资
     * @param code
     */
    public void queryImpaByCode(String code)
    {
        if( code.length() > 35 )
        {
            return;
        }
        ApiService.Builder.getChanBoServer()
                          .getImpaByCode(code)
                          .compose(RxSchedulersUtils.Obs_io_main())
                          .subscribe(new VMObserver<ApiResult<ImpaBean>>() {
                              @Override
                              public void onStart(Disposable d)
                              {
                                addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getImpaResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<ImpaBean> result)
                              {
                                  if( result.isOk() )
                                  {
                                      getImpaResult().postValue( new LiveResult<>( true , result.getData() ) );
                                  }
                                  else
                                  {
                                      getImpaResult().postValue( new LiveResult<>( false ,result.getMessage() ) );
                                  }
                              }
                          });

    }


    private MutableLiveData<LiveResult<Integer>> delRukuResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<Integer>> getDelRukuResult() {
        return delRukuResult;
    }

    private void deleteRukuGoodsLogByUuid(String[] uuids)
    {
        ApiService.Builder.getChanBoServer()
                          .deleteRukuGoodsLogByUuid(uuids)
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<ApiResult<Object>>()
                          {
                              @Override
                              public void onStart(Disposable d)
                              {
                                addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getDelRukuResult().postValue(new LiveResult<>(false,errorMsg));
                              }

                              @Override
                              public void onSuccess(ApiResult<Object> result)
                              {
                                  //{"status":true,"code":"316","message":"删除成功","data":null}
                                  if( result.getCode().equals("316")  )
                                  {
                                      getDelRukuResult().postValue(new LiveResult<>(true,result.getMessage()));
                                  }
                                  else
                                  {
                                      getDelRukuResult().postValue(new LiveResult<>(false,result.getMessage()));
                                  }
                              }
                          });

    }

    /**
     * 删除 入库的 物资
     * @param mUuid
     */
    public void deleteRukuLogByUuid(String mUuid)
    {
        String[] uidS = new String[]{mUuid};
        deleteRukuGoodsLogByUuid(uidS);
    }


}
